package com.controller;

import com.mapper.entity.StockCirculation;
import com.mapper.entity.StockCirculationDetailOrg;
import com.mapper.entity.mapper.DealerMapper;
import com.mapper.entity.mapper.OrderMapper;
import com.mapper.entity.mapper.StockCirculationDetailMapper;
import com.mapper.entity.mapper.StockCirculationMapper;
import com.service.StockDetailInsertHelper;
import com.service.impl.HandleStockOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import tk.mybatis.mapper.weekend.Weekend;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud.controller
 * @date:2021/4/19
 */
@Controller
public class TestController {


    @Autowired
    DealerMapper dealerMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StockCirculationMapper stockCirculationMapper;

    @Autowired
    StockCirculationDetailMapper stockCirculationDetailMapper;

    @Autowired
    HandleStockOrg handleStockOrg;

    @Autowired
    StockDetailInsertHelper stockDetailInsertHelper;

    Integer weight = 1;
    static final int nThreads = Runtime.getRuntime().availableProcessors();

    public Integer poolSize = 4;

    public ThreadPoolExecutor pool = new ThreadPoolExecutor(poolSize, poolSize,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());

    @GetMapping(value = "create")
    public void test() throws Exception {

//        Date d1 = new Date();
//        File file2 = new File("/Users/libin/Desktop/分类3/错误");
////
////        File file2 = new File("/Users/libin/Desktop/分类3/错误2");
//        List<String> allList = new ArrayList<>();
//        List<File> fileList = new ArrayList<>();
////
////        Set<String> set1 = new HashSet<>();
////        Scanner scanner = new Scanner(file);
////        while (scanner.hasNextLine()) {
////            set1.add(scanner.nextLine());
////        }
////
////        Scanner scanner1 = new Scanner(file2);
////        while (scanner1.hasNext()) {
////
////            String error = scanner1.nextLine();
////            StockCirculationDetailOrg wrap = handleStockOrg.wrap(error);
////            if (!set1.contains(wrap.get_barcode_content())) {
////                allList.add(error);
////            }
////        }
////        InsertStockAndDetailTask task = new InsertStockAndDetailTask();
////        task.setStockCirculationDetailMapper(stockCirculationDetailMapper);
////        task.setErrorFile(file2);
////        task.setHandleStockOrg(handleStockOrg);
////        task.setList(allList);
////        task.setStockCirculationMapper(stockCirculationMapper);
////        task.setStockDetailInsertHelper(stockDetailInsertHelper);
////        pool.execute(task);
//
//        for (int i = 0; i < 4; i++) {
//            File file = new File("/Users/libin/Desktop/分类2/8" + i);
//            fileList.add(file);
//        }
////
////
//////        Set set = new HashSet();
//        for (File file : fileList) {
//            Scanner scanner = new Scanner(file);
//            while (scanner.hasNextLine()) {
//                String string = scanner.nextLine();
////                JSONObject json = JSONUtil.parseObj(string);
////                String code = json.getStr("_barcode_content");
////                set.add(code+"\r\n");
//                allList.add(string);
//            }
//        }
//
//        List<List< String>> splits = Lists.partition(allList,2000);
////
//
//        List<InsertStockAndDetailTask> list = new ArrayList<>();
//        for(List<String> thisList :splits){
//            InsertStockAndDetailTask task = new InsertStockAndDetailTask();
//            task.setStockCirculationDetailMapper(stockCirculationDetailMapper);
//            task.setErrorFile(file2);
//            task.setHandleStockOrg(handleStockOrg);
//            task.setList(thisList);
//            task.setStockCirculationMapper(stockCirculationMapper);
//            task.setStockDetailInsertHelper(stockDetailInsertHelper);
//            list.add(task);
//        }
//
//
////        Date date = new Date() ;
////        String line = "{\"_id\":\"56532655\",\"_barcode_content\":\"HTTPS://SJF9.CN/X/*C$*BTC1N213HAS-KM7I\",\"_status\":\"12\",\"_operate_time\":\"2021-09-1510:22:05\",\"_operate_name\":\"泽富烟酒\",\"_organization_type\":\"2\",\"_organization_code\":\"035213\",\"_organization_name\":\"泽富烟酒\",\"_product_code\":\"1200000340\",\"_dealers_type\":\"2\",\"_dealers_code\":\"\",\"_dealers_name\":\"\",\"_product_name\":\"38度500mL*6水井坊.臻酿八号\",\"_batch_code\":\"\",\"_product_line_code\":\"\",\"_product_line_name\":\"\",\"_workshop_code\":\"\",\"_workshop_name\":\"\",\"_product_team_code\":\"\",\"_product_team\":\"\",\"_warehouse_code\":\"\",\"_warehouse_name\":\"\",\"_towarehouse_code\":\"\",\"_towarehouse_name\":\"\",\"_workid\":\"DD321091559143\",\"_synchronization_status\":\"0\",\"_batch_no\":\"\"}\n";
////        StockCirculationDetailOrg wrap = handleStockOrg.wrap(line);
////        StockCirculation stockCirculation = handleStockOrg.Instance(wrap);
////        stockCirculationMapper.insertReturnId(stockCirculation);
////        StockCirculationDetail stockCirculationDetail = handleStockOrg.InstanceDetail(wrap);
////        stockCirculationDetailMapper.insertSelectiveReturnId(stockCirculationDetail);
////        Weekend<StockCirculation> stockCirculationWeekend2 = Weekend.of(StockCirculation.class);
////        stockCirculationWeekend2.createCriteria().andEqualTo("barcodeContent", stockCirculation.getBarcodeContent()).andEqualTo("id", stockCirculation.getId());
////        stockCirculation.setLinkId(stockCirculationDetail.getLinkId());
////        stockCirculationMapper.updateByExampleSelective(stockCirculation, stockCirculationWeekend2);
//
//
////        stockCirculationDetail.setResourceId(stockCirculation.getId());
////        stockDetailInsertHelper.updateDetail(stockCirculation.getStatus(),stockCirculation.getBarcodeContent(),stockCirculationDetail);
//
//
//
//
//        while (true) {
//            System.out.println("queueSize-"+pool.getQueue().size());
//            System.out.println("pollSize-"+pool.getPoolSize());
//            System.out.println("active-"+pool.getActiveCount());
//            if(){
//
//            }
//            Thread.sleep(5000L);
//        }

    }


    public static void main(String[] arg) throws Exception {

        String s1 = "spring.shardingsphere.datasource.stockNNN.driver-class-name=com.mysql.jdbc.Driver";
        String s2 = "spring.shardingsphere.datasource.stockNNN.type=com.alibaba.druid.pool.DruidDataSource";
        String s3 = "spring.shardingsphere.datasource.stockNNN.url=jdbc:mysql://localhost:3306/stock_NNN";
        String s4 = "spring.shardingsphere.datasource.stockNNN.username=root";
        String s5 = "spring.shardingsphere.datasource.stockNNN.password=123456";

        File file = new File("/Users/libin/Desktop/数据源");
        List<String> list = new ArrayList<>();
        for (int i = 2; i < 43; i++) {
            String thisS1 = s1.replaceAll("NNN", String.valueOf(i)) + "\r\n";
            String thisS2 = s2.replaceAll("NNN", String.valueOf(i)) + "\r\n";
            String thisS3 = s3.replaceAll("NNN", String.valueOf(i)) + "\r\n";
            String thisS4 = s4.replaceAll("NNN", String.valueOf(i)) + "\r\n";
            String thisS5 = s5.replaceAll("NNN", String.valueOf(i)) + "\r\n";
            list.add(thisS1);
            list.add(thisS2);
            list.add(thisS3);
            list.add(thisS4);
            list.add(thisS5);

        }
        writeFile(file.getAbsolutePath(), list);
    }

    public static void writeFile(String filePath, Collection<String> lineList) throws Exception {
        File copy = new File(filePath.substring(0, filePath.lastIndexOf("/")));
//        File copy = new File(filePath);
        if (!copy.exists()) {
            copy.mkdirs();
        }
        try {
            FileOutputStream fout = new FileOutputStream(filePath, true);
            for (String string : lineList) {
                fout.write(string.getBytes("utf-8"));
            }
            fout.flush();
            fout.close();
        } catch (Exception e) {
            throw e;
        }
    }


    @GetMapping(value = "delete")
    public void delete() throws Exception {

//        Date d1 = new Date();
        File file2 = new File("/Users/libin/Desktop/分类3/错误");
//
//        File file2 = new File("/Users/libin/Desktop/分类3/错误2");
        List<String> allList = new ArrayList<>();
        List<File> fileList = new ArrayList<>();
//
//        Set<String> set1 = new HashSet<>();
//        Scanner scanner = new Scanner(file);
//        while (scanner.hasNextLine()) {
//            set1.add(scanner.nextLine());
//        }
//
//        Scanner scanner1 = new Scanner(file2);
//        while (scanner1.hasNext()) {
//
//            String error = scanner1.nextLine();
//            StockCirculationDetailOrg wrap = handleStockOrg.wrap(error);
//            if (!set1.contains(wrap.get_barcode_content())) {
//                allList.add(error);
//            }
//        }
//        InsertStockAndDetailTask task = new InsertStockAndDetailTask();
//        task.setStockCirculationDetailMapper(stockCirculationDetailMapper);
//        task.setErrorFile(file2);
//        task.setHandleStockOrg(handleStockOrg);
//        task.setList(allList);
//        task.setStockCirculationMapper(stockCirculationMapper);
//        task.setStockDetailInsertHelper(stockDetailInsertHelper);
//        pool.execute(task);

        for (int i = 0; i < 5; i++) {
            File file = new File("/Users/libin/Desktop/分类2/6" + i);
            fileList.add(file);
        }


//        Set set = new HashSet();
        for (File file : fileList) {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String string = scanner.nextLine();
//                JSONObject json = JSONUtil.parseObj(string);
//                String code = json.getStr("_barcode_content");
//                set.add(code+"\r\n");
                allList.add(string);
            }
        }

       for(String line :allList){
           StockCirculationDetailOrg wrap = handleStockOrg.wrap(line);
           StockCirculation stockCirculation = handleStockOrg.Instance(wrap);


           Weekend<StockCirculation> weekend = Weekend.of(StockCirculation.class);
           weekend.createCriteria().andEqualTo("barcodeContent",stockCirculation.getBarcodeContent());

           List<StockCirculation> list = stockCirculationMapper.selectByExample(weekend);
            stockCirculationMapper.deleteByExample(weekend);
           list.stream().forEach(stockCirculation1 -> {
               stockDetailInsertHelper.delete(stockCirculation.getStatus(), stockCirculation.getBarcodeContent(), stockCirculation1.getLinkId());
           });


       }

    }
}
